// 1、无头单向非循环链表实现
public class SingleLinkedList implements IList {
    static class ListNode {
        public int val;
        public ListNode next;

        //提供一个构造方法
        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null){
            this.head = node;
        }else {
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if (this.head == null){
            this.head = node;
        }else {
            while (cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) throws PosIllegality {
        if (index < 0 || index > size()){
            throw new PosIllegality("插入位置不合法！"+index);
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = searchPrev(index);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        int count = 0;
        while (count != index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }else {
                cur = cur.next;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (this.head == null){
            return;
        }
        if (this.head.val == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = findPrev(key);
        if (cur == null){
            System.out.println("没有你要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findPrev(int key) {
        ListNode cur = this.head;
        while (cur.next != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key){
            head = head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //从指定位置开始打印
    public void display(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while (cur != null){
            ListNode curNext = this.head.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
    }

    //反转链表
    public ListNode reverseList() {
        if (head == null) {
            return null;
        }
        if (head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    //找中间节点
    public ListNode midNode() {
        int len = size();
        ListNode cur = this.head;
        for (int i = 0; i < len / 2; i++) {
            cur = cur.next;
        }
        return cur;
    }

    public ListNode midNode2() {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //输出倒数第k个节点
    public ListNode findKthToTail2(int k) {
        if (k <= 0 || k > size() || head == null) {
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while (k-1 != 0){
            fast = fast.next;
            k--;
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    //优化，不size（），考虑到k非常大的情况
    public ListNode findKthToTail(int k) {
        if (k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while (k-1 != count){
            fast = fast.next;
            if (fast == null){
                return null;
            }
            count++;
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    //判断是否为回文结构
    public boolean chkPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        //1.找到中间节点
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.翻转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3.从前到后，从后到前
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            //考虑偶数情况
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    //输入两个链表，找出它们的第一个公共结点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode cur1 = headA;
        int len1 = 0;
        while (cur1 != null) {
            cur1 = cur1.next;
            len1++;
        }
        ListNode cur2 = headB;
        int len2 = 0;
        while (cur2 != null) {
            cur2 = cur2.next;
            len2++;
        }
        int len = len1 - len2;
        cur1 = headA;
        cur2 = headB;
        if (len < 0) {
            cur1 = headB;
            cur2 = headA;
            len = len2 - len1;
        }
        while (len != 0) {
            cur1 = cur1.next;
            len--;
        }
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        if (cur1 == null) {
            return null;
        }
        return cur1;
    }

    //判断链表带环
    public boolean hasCycle2(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return false;
        }
        return true;
    }

    //现有一链表的头指针 ListNode* pHead，
    //给一定值x，编写一段代码将所有小于x的结点排在其余结点之前，
    //且不能改变原来的数据顺序，返回重新排列后的链表的头指针
    public ListNode partition(ListNode pHead, int x) {
        if (pHead == null) {
            return null;
        }
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    //插入第一个节点
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //开始串起来
        if (bs == null) {
            //第一个区间没有数据
            return as;
        }
        //第一个区间有数据
        be.next = as;
        if (as != null) {
            //第二个区间有数据
            ae.next = null;
        }
        return bs;
    }
}
